/**********************************************************************//**
  \file WSNSensorManager.c

  \brief

  \author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2012, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
  History:
    13/06/07 I. Kalganova - Modified

  Last change:
    $Id: WSNSensorManager.c 20608 2012-03-23 09:22:15Z arazinkov $
**************************************************************************/

/*****************************************************************************
                              Includes section
******************************************************************************/
#include <types.h>
#include <taskManager.h>
#include <sensors.h>
#include <WSNDemoApp.h>
#include <WSNVisualizer.h>

/*****************************************************************************
                              External variables section
******************************************************************************/
extern AppNwkInfoCmdPayload_t appNwkInfo;

/*****************************************************************************
                              Local variables section
******************************************************************************/
static void (*callback)(void);

/****************************************************************************
                              Static functions prototypes section
******************************************************************************/
static void temperatureDataReady(bool result, int16_t temperature);
static void pressureDataReady(bool result, uint32_t pressure);
static void humidityDataReady(bool result, uint16_t humidity);
static void uvDataReady(bool result, uint16_t uv);
static void pvDataReady(bool result, uint16_t pv);
static void windDirectionDataReady(bool result, uint16_t windDir);
static void windSpeedDataReady(uint16_t windSpeed);
static void batteryDataReady(uint16_t battery);

/******************************************************************************
                              Implementations section
******************************************************************************/
/**************************************************************************//**
  \brief Sensors initialization.

  \param None.

  \return None
******************************************************************************/
void appStartSensorManager(void)
{
  BSP_OpenTemperatureSensor();
  BSP_OpenPressureSensor();
  BSP_OpenHumiditySensor();
  BSP_OpenUVSensor();
  BSP_OpenPVSensor();
  BSP_OpenWindDirectionSensor();
  BSP_OpenWindSpeedSensor();
  BSP_OpenBatterySensor();
  BSP_OpenAdc();
}

/**************************************************************************//**
  \brief Closes all sensors.

  \param None.

  \return None
******************************************************************************/
void appStopSensorManager()
{
  BSP_CloseTemperatureSensor();
  BSP_ClosePressureSensor();
  BSP_CloseHumiditySensor();
  BSP_CloseUVSensor();
  BSP_ClosePVSensor();
  BSP_CloseWindDirectionSensor();
  BSP_CloseWindSpeedSensor();
  BSP_CloseBatterySensor();
  BSP_CloseAdc();
}

/**************************************************************************//**
  \brief Sensors data request.

  \param[in] sensorsGot - on sensors reading finished callback.

  \return None.
******************************************************************************/
void appGetSensorData(void (*sensorsGot)(void))
{
  callback = sensorsGot;
  BSP_ReadTemperatureData(temperatureDataReady);
  //BC_SUCCESS == BSP_ReadPressureTemp(temperatureDataReady);
}

/**************************************************************************//**
  \brief Temperature reading done callback.

  \param[in] result - successful temperature reading flag.
  \param[in] temperature - result of temperature reading, if result is true.

  \return None.
******************************************************************************/
static void temperatureDataReady(bool result, int16_t temperature)
{
  if (result)
    appNwkInfo.boardInfo.alcestation.temperature = CPU_TO_LE16(temperature);
  else
    appNwkInfo.boardInfo.alcestation.temperature = CPU_TO_LE16(0);

  if (APP_DEVICE_TYPE == DEV_TYPE_COORDINATOR)
    BSP_ReadWindDirectionData(windDirectionDataReady);
  else
    BSP_ReadPressureData(pressureDataReady);

  //callback();
}

/**************************************************************************//**
  \brief Pressure reading done callback.

  \param[in] result - successful pressure reading flag.
  \param[in] pressure - result of pressure reading, if result is true.

  \return None.
******************************************************************************/
static void pressureDataReady(bool result, uint32_t pressure)
{
  if (result)
    appNwkInfo.boardInfo.alcestation.pressure = CPU_TO_LE32(pressure);
  else
    appNwkInfo.boardInfo.alcestation.pressure = CPU_TO_LE32(0);

	BSP_ReadHumidityData(humidityDataReady);
	//callback();
}

/**************************************************************************//**
  \brief Pressure reading done callback.

  \param[in] result - successful pressure reading flag.
  \param[in] humidity - result of pressure reading, if result is true.

  \return None.
******************************************************************************/
static void humidityDataReady(bool result, uint16_t humidity)
{
	if (result)
		appNwkInfo.boardInfo.alcestation.humidity = CPU_TO_LE16(humidity);
	else
		appNwkInfo.boardInfo.alcestation.humidity = CPU_TO_LE16(0);

	BSP_ReadUVData(uvDataReady);
	//callback();
}

/**************************************************************************//**
  \brief UV reading done callback.

  \param[in] result - successful uv reading flag.
  \param[in] uv - result of uv reading, if result is true.

  \return None.
******************************************************************************/
static void uvDataReady(bool result, uint16_t uv)
{
	if (result)
		appNwkInfo.boardInfo.alcestation.uv = CPU_TO_LE16(uv);
	else
		appNwkInfo.boardInfo.alcestation.uv = CPU_TO_LE16(0);

	BSP_ReadPVData(pvDataReady);
	//callback();
}

/**************************************************************************//**
  \brief PV reading done callback.

  \param[in] result - successful pv reading flag.
  \param[in] pv - result of pv reading, if result is true.

  \return None.
******************************************************************************/
static void pvDataReady(bool result, uint16_t pv)
{
	if (result)
		appNwkInfo.boardInfo.alcestation.pv = CPU_TO_LE16(pv);
	else
		appNwkInfo.boardInfo.alcestation.pv = CPU_TO_LE16(0);

	BSP_ReadBatteryData(batteryDataReady);
	//callback();
}

/**************************************************************************//**
  \brief Wind Direction reading done callback.

  \param[in] result - successful wind direction reading flag.
  \param[in] windDir - result of wind direction reading, if result is true.

  \return None.
******************************************************************************/
static void windDirectionDataReady(bool result, uint16_t windDir)
{
	if (result)
		appNwkInfo.boardInfo.alcestation.windDir = CPU_TO_LE16(windDir);
	else
		appNwkInfo.boardInfo.alcestation.windDir = CPU_TO_LE16(0);

	BSP_ReadWindSpeedData(windSpeedDataReady);
	//callback();
}

/**************************************************************************//**
  \brief Wind Speed reading done callback.

  \param[in] result - successful wind speed reading flag.
  \param[in] windSpeed - result of wind speed reading, if result is true.

  \return None.
******************************************************************************/
static void windSpeedDataReady(uint16_t windSpeed)
{
	appNwkInfo.boardInfo.alcestation.windSpeed = CPU_TO_LE16(windSpeed);
	BSP_ReadBatteryData(batteryDataReady);
	//callback();
}

/**************************************************************************//**
  \brief Battery level reading done callback.

  \param[in] battery - result of battery level reading, if result is true.

  \return None.
******************************************************************************/
static void batteryDataReady(uint16_t battery)
{
  appNwkInfo.boardInfo.alcestation.battery = CPU_TO_LE16(battery);
  callback();
}

//eof WSNSensorManager.c
